package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsService;
import com.qf.vo.GenVo;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * projectName:temp_shop
 * author:luo
 * time:2023/2/25 14:53
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;
    @Autowired
    private DtsGoodsAttributeMapper dtsGoodsAttributeMapper;
    @Autowired
    private DtsGoodsSpecificationMapper dtsGoodsSpecificationMapper;
    @Autowired
    private DtsGoodsProductMapper dtsGoodsProductMapper;
    @Autowired
    private DtsCommentMapper dtsCommentMapper;
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Override
    public IPage<DtsGoods> findGoodsList(Integer page, Integer limit, String sort, String order, String goodsSn, String name) {
        return dtsGoodsMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsGoods>()
                .like(!StringUtils.isEmpty(goodsSn),"goods_sn",goodsSn)
                .like(!StringUtils.isEmpty(name),"name",name)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public List<Integer> findCategoryIdsById(Integer goodsId) {
        List<Integer>  id= dtsGoodsMapper.selectList(new QueryWrapper<DtsGoods>()
                .eq("id", goodsId)).stream().map(DtsGoods::getCategoryId).collect(Collectors.toList());
        List<Integer> categoryIds = dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>()
                .eq("id", id)).stream().map(DtsCategory::getId).collect(Collectors.toList());
        categoryIds.add(id.get(0));
        return categoryIds;
    }

    @Override
    public DtsGoods findGoodById(Integer goodsId) {
        return dtsGoodsMapper.selectOne(new QueryWrapper<DtsGoods>().eq("id",goodsId));
    }

    @Override
    public List<DtsGoodsAttribute> findAttributeByGoodsId(Integer goodsId) {
        return dtsGoodsAttributeMapper.selectList(new QueryWrapper<DtsGoodsAttribute>()
                .eq("goods_id",goodsId));
    }

    @Override
    public List<DtsGoodsSpecification> findSpecificationsByGoodsId(Integer goodsId) {
        return dtsGoodsSpecificationMapper.selectList(new QueryWrapper<DtsGoodsSpecification>()
                .eq("goods_id",goodsId));
    }

    @Override
    public List<DtsGoodsProduct> findProductByGoodsId(Integer goodsId) {
        return dtsGoodsProductMapper.selectList(new QueryWrapper<DtsGoodsProduct>()
                .eq("goods_id",goodsId));
    }

    @Override
    public List<GenVo> findBrandListAll() {
        return dtsBrandMapper.selectList(null).stream().map(dtsBrand -> {
            GenVo brandVo = new GenVo();
            brandVo.setValue(dtsBrand.getId());
            brandVo.setLabel(dtsBrand.getName());
            return brandVo;
        }).collect(Collectors.toList());
    }

    @Override
    public void modifyGoods(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        goods.setUpdateTime(new Date());
        dtsGoodsMapper.updateById(goods);
        Arrays.stream(attributes).map(dtsGoodsAttribute -> {
            dtsGoodsAttribute.setUpdateTime(new Date());
            dtsGoodsAttributeMapper.updateById(dtsGoodsAttribute);
            return dtsGoodsAttribute;
        });
        Arrays.stream(products).map(dtsGoodsProduct -> {
            dtsGoodsProduct.setUpdateTime(new Date());
            dtsGoodsProductMapper.updateById(dtsGoodsProduct);
            return dtsGoodsProduct;
        });
        Arrays.stream(specifications).map(dtsGoodsSpecification -> {
            dtsGoodsSpecification.setUpdateTime(new Date());
            dtsGoodsSpecificationMapper.updateById(dtsGoodsSpecification);
            return dtsGoodsSpecification;
        });

    }

    @Override
    public void removeGoods(DtsGoods goods) {
        dtsGoodsMapper.deleteById(goods);
    }

    @Override
    public void addGoods(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        goods.setAddTime(new Date());
        dtsGoodsMapper.insert(goods);
        Arrays.stream(attributes).map(dtsGoodsAttribute -> {
            dtsGoodsAttribute.setAddTime(new Date());
            dtsGoodsAttributeMapper.insert(dtsGoodsAttribute);
            return dtsGoodsAttribute;
        });
        Arrays.stream(products).map(dtsGoodsProduct -> {
            dtsGoodsProduct.setAddTime(new Date());
            dtsGoodsProductMapper.insert(dtsGoodsProduct);
            return dtsGoodsProduct;
        });
        Arrays.stream(specifications).map(dtsGoodsSpecification -> {
            dtsGoodsSpecification.setAddTime(new Date());
            dtsGoodsSpecificationMapper.insert(dtsGoodsSpecification);
            return dtsGoodsSpecification;
        });
    }

    @Override
    public IPage<DtsComment> findCommentList(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        return dtsCommentMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsComment>()
                .eq(!StringUtils.isEmpty(userId),"user_id",userId)
                .eq(!StringUtils.isEmpty(valueId),"value_id",valueId)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public void removeComment(DtsComment dtsComment) {
        dtsCommentMapper.deleteById(dtsComment);
    }
}
